package ru.spies.content;

import java.text.SimpleDateFormat;
import java.util.Collection;

import ru.spies.dbwork.DBQueries;
import ru.spies.dbwork.DBWork;
import ru.spies.entities.Agent;
import ru.spies.entities.Country;
import ru.spies.entities.Department;
import ru.spies.entities.Document;
import ru.spies.entities.Importance;
import ru.spies.entities.Task;
import ru.spies.entities.TaskType;
import ru.spies.profileClasses.UserInfo;

public class DeleteContent extends AbstractContent {

	private static DeleteContent statThis=null;
	
	public DeleteContent() {
	}

	public static DeleteContent getInstance(){
		if (statThis==null){
			statThis = new DeleteContent();
		}
		return statThis;
	}
	
	public String formDeleteCountryContent(){
		StringBuffer htmlContent = new StringBuffer(createCheckBox("withCountry")+"<p>Имя страны:</p><select name=\"countryName\" class=\"txtBox\">");
		Collection<Country> countries=(Collection<Country>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_COUNTRY_FIND);
		if (countries.size()!=0){
			for (Country country : countries) {
				htmlContent.append(getComboOption(country.getName(),country.getId().toString()));
			}
		} else {
			return COUNTRY_ERROR;
		}
		htmlContent.append("</select>"+createCheckBox("withRelation")+"<p>Отношения:</p><select name=\"relation\"  class=\"txtBox\"><option value=\"friend\">Дружеские</option><option value=\"neutral\">Нейтральные</option><option value=\"enemy\">Вражеские</option></select><input name=\"send\" type=\"submit\" class=\"inFormBtn\" value=\"Отправить\">");		
		return htmlContent.toString();
	}

	public String formDeleteDepartmentContent(){
		StringBuffer htmlContent = new StringBuffer(createCheckBox("withName")+"<p>Наименование отдела:</p><select name=\"depName\" class=\"txtBox\">");
		Collection<Department> departments = (Collection<Department>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_DEPARTMENT_FIND);
		if (departments.size()!=0){
			for (Department department : departments) {
				htmlContent.append(getComboOption(department.getName(), department.getId().toString()));
			}
		} else {
			return DEPARTMENT_ERROR;
		}
		htmlContent.append("</select>"+createCheckBox("withCountry")+"<p>Страна:</p><select name=\"countryName\"  class=\"txtBox\">");
		Collection<Country> countries = (Collection<Country>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_COUNTRY_FIND);
		if (countries.size()!=0){
			for (Country country : countries) {
				htmlContent.append(getComboOption(country.getName(),country.getId().toString()));
			}
		}
		htmlContent.append("</select>"+createCheckBox("withResident")+"<p>Глава отдела:</p><select name=\"idResident\"  class=\"txtBox\"><option value=\"null\">Отсутствует</option>");
		Collection<Agent> agents = (Collection<Agent>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_AGENT_FIND);
		if (agents.size()!=0){
			for (Agent agent : agents) {
				htmlContent.append(getComboOption(agent.getName(),agent.getId().toString()));
			}
		}
		htmlContent.append("</select>"+createCheckBox("withResident")+"<p>Дата создания:</p><select name=\"creationDate\"  class=\"txtBox\">");
		SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
		for (Department department : departments) {
			String strDate=sdf.format(department.getCreationDate());
			htmlContent.append(getComboOption(strDate,strDate));
		}
		htmlContent.append("</select><input name=\"send\" type=\"submit\" class=\"inFormBtn\" value=\"Отправить\">");
		return htmlContent.toString();
	}
	
	public String formDeleteImportanceContent(){
		StringBuffer htmlContent = new StringBuffer("<p>Тип секретности:</p><select name=\"importanceName\" class=\"txtBox\" >");
		Collection<Importance> importances=(Collection<Importance>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_IMPORTANCE_FIND);
		if (importances.size()!=0){
			for (Importance importance : importances) {
				htmlContent.append(getComboOption(importance.getType(),importance.getId().toString()));
			}
		} else {
			return IMPORTANCE_ERROR;
		}
		htmlContent.append("</select><input name=\"send\" type=\"submit\" class=\"inFormBtn\" value=\"Отправить\">");
		return htmlContent.toString();
	}
	
	public String formDeleteTaskTypeContent(){
		StringBuffer htmlContent = new StringBuffer("<p>Тип задания:</p><select name=\"TaskTypeName\" class=\"txtBox\" >");
		Collection<TaskType> taskTypes=(Collection<TaskType>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_TASK_TYPE_FIND);
		if (taskTypes.size()!=0){
			for (TaskType taskType : taskTypes) {
				htmlContent.append(getComboOption(taskType.getTypeName(),taskType.getId().toString()));
			}
		} else {
			return TASK_TYPE_ERROR;
		}
		htmlContent.append("</select><input name=\"send\" type=\"submit\" class=\"inFormBtn\" value=\"Отправить\">");
		return htmlContent.toString();
	}

	public String formDeleteAgentAndTaskContent(){
		Collection<Agent> agents= (Collection<Agent>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_AGENT_FIND);
		Collection<Task> tasks = (Collection<Task>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_TASK_FIND);
		if (agents.size()==0){
			return AGENT_ERROR;
		}
		if (tasks.size()==0){
			return TASK_ERROR;
		}
		boolean minOneAgent=false;
		boolean minOneTask=false;
		StringBuffer htmlContent=new StringBuffer(createCheckBox("withAgent")+"<p>Агент:</p><select name=\"agent\"  class=\"txtBox\">");
		for (Agent agent:agents){
			if (checkAgentForMineDepartment(agent)){
				htmlContent.append(getComboOption(agent.getName(), agent.getId().toString()));
				minOneAgent=true;
			}
		}
		if (!minOneAgent){
			return AGENT_ERROR_FOR_RESIDENT;
		}
		htmlContent.append("</select>"+createCheckBox("withTask")+"<p>Задание:</p><select name=\"task\"  class=\"txtBox\">");
		for (Task task:tasks){
			if (checkTaskForMineCountry(task)){
				htmlContent.append(getComboOption(task.getName(), task.getId().toString()));
				minOneTask=true;
			}
		}
		if (!minOneTask){
			return TASK_ERROR_FOR_RESIDENT;
		}
		htmlContent.append("</select><input name=\"send\" type=\"submit\" class=\"inFormBtn\" value=\"Отправить\">");
		return htmlContent.toString();
	}

	public String formDeleteAgentContent(){
		StringBuffer htmlContent = new StringBuffer(createCheckBox("withName")+"<p>Имя агента:</p><select name=\"agentName\" class=\"txtBox\">");
		Collection<Agent> agents = (Collection<Agent>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_AGENT_FIND);
		boolean minOneAgent=false;
		if (agents.size()!=0){
			for (Agent agent : agents) {
				if (checkAgentForMineDepartment(agent)){
					htmlContent.append(getComboOption(agent.getName(),agent.getId().toString()));
					minOneAgent=true;
				}
			}
		} else {
			return AGENT_ERROR;
		}
		if (!minOneAgent){
			return AGENT_ERROR_FOR_RESIDENT;
		}
		htmlContent.append("</select>"+createCheckBox("withDepartment")+"<p>Отдел:</p><select name=\"idDepartment\"  class=\"txtBox\">");
		Collection<Department> departments = (Collection<Department>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_DEPARTMENT_FIND);
		if (departments.size()!=0){
			for (Department department : departments) {
				if (department.getId().intValue()==UserInfo.getInstance().getMe().getAgent().getDepartment().getId().intValue()){
					htmlContent.append(getComboOption(department.getName(),department.getId().toString()));
				}
			}
		}
		htmlContent.append("</select>"+createCheckBox("withRecruiter")+"<p>Вербовщик:</p><select name=\"idRecruiter\"  class=\"txtBox\"><option value=\"null\">Отсутствует</option>");
		for (Agent agent : agents){
			if (checkAgentForMineDepartment(agent)){
				Agent recruiter=agent.getRecruiter();
				if (recruiter!=null){
					htmlContent.append(getComboOption(recruiter.getName(),recruiter.getId().toString()));
				}
			}
		}
		htmlContent.append("</select>");
		if (UserInfo.getInstance().getPrivileges().canDoNormalDelete()){
			htmlContent.append(createCheckBox("withCountRecruits")+"<p>Количество завербованых агентов:</p><select name=\"countRecruits\" class=\"txtBox\">");
			for (int i=0;i<9;i++){
				htmlContent.append(getComboOption(String.valueOf(i), String.valueOf(i)));
			}
			htmlContent.append("</select>");
		}	
		htmlContent.append(createCheckBox("withStatus")+"<p>Раскрыт?</p><select name=\"status\" class=\"txtBox\">");
		htmlContent.append(getComboOption("Да","true"));
		htmlContent.append(getComboOption("Нет", "false"));
		htmlContent.append("</select><input name=\"send\" type=\"submit\" class=\"inFormBtn\" value=\"Отправить\">");
		return htmlContent.toString();
	}

	public String formDeleteTaskContent(){
		Collection<Task> tasks = (Collection<Task>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_TASK_FIND);
		if (tasks.size()==0){
			return TASK_ERROR;
		}
		StringBuffer htmlContent=new StringBuffer(createCheckBox("withName")+"<p>Наименование задания:</p><select name=\"taskName\" class=\"txtBox\">");
		for (Task task : tasks) {
			htmlContent.append(getComboOption(task.getName(),task.getId().toString()));
		}
		htmlContent.append("</select>"+createCheckBox("withImportance")+"<p>Тип секретности:</p><select name=\"idImportance\"  class=\"txtBox\">");
		Collection<Importance> importances= (Collection<Importance>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_IMPORTANCE_FIND);
		Collection<TaskType> tasktypes = (Collection<TaskType>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_TASK_TYPE_FIND);
		for (Importance importance:importances){
			htmlContent.append(getComboOption(importance.getType(), importance.getId().toString()));
		}
		htmlContent.append("</select>"+createCheckBox("withTaskType")+"<p>Тип задания:</p><select name=\"idTaskType\"  class=\"txtBox\"><option value=\"null\">Пока что отсутствует</option>");
		if (tasktypes.size()!=0){
			for (TaskType task:tasktypes){
				htmlContent.append(getComboOption(task.getTypeName(), task.getId().toString()));
			}
		}
		htmlContent.append("</select>"+createCheckBox("withCountry")+"<p>Страна, связанная с заданием:</p><select name=\"idCountry\"  class=\"txtBox\">");
		Collection<Country> countries = (Collection<Country>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_COUNTRY_FIND);
		for (Country country: countries){
			htmlContent.append(getComboOption(country.getName(), country.getId().toString()));
		}
		htmlContent.append("</select>"+createCheckBox("withStatus")+"<p>Статус задания:</p><select class=\"txtBox\" name=\"status\">");
		String statusValue[]={"a","c","f"};
		String statusName[]={"Активно","Выполнено","Провалено"};
		for(int i=0;i<3;i++){
			htmlContent.append(getComboOption(statusName[i], statusValue[i]));
		}
		htmlContent.append("</select><input name=\"send\" type=\"submit\" class=\"inFormBtn\" value=\"Отправить\">");
		return htmlContent.toString();
	}

	public String formDeleteDocumentContent(){
		Collection<Document> documents= (Collection<Document>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_DOCUMENT_FIND);	
		if (documents.size()==0){
			return DOCUMENT_ERROR;
		}
		Collection<Importance> importances= (Collection<Importance>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_IMPORTANCE_FIND);	
		if (importances.size()==0){
			return IMPORTANCE_ERROR;
		}
		Collection<Task> tasks = (Collection<Task>) DBWork.getInstance().getTableObjects(DBQueries.QUERY_TASK_FIND);
		StringBuffer htmlContent=new StringBuffer(createCheckBox("withTextLink")+"<p>Ссылка на файл:</p><select name=\"textLink\" class=\"txtBox\" type=\"text\"/>");
		boolean minOneDoc=false;
		for(Document document:documents){
			if (isThisDocumentMineOrFromMineDepartment(document)){
				htmlContent.append(getComboOption(document.getTextLink(),document.getId().toString()));
				minOneDoc=true;
			}
		}
		if (!minOneDoc){
			return DOCUMENT_ERROR_FOR_AGENT;
		}
		htmlContent.append("</select>"+createCheckBox("withImportance")+"<p>Тип секретности:</p><select name=\"importance\"  class=\"txtBox\">");
		for (Importance importance:importances){
			htmlContent.append(getComboOption(importance.getType(), importance.getId().toString()));
		}
		htmlContent.append("</select><p>Задание:</p><select name=\"task\"  class=\"txtBox\"><option value=\"null\">Пока что отсутствует</option>");
		if (tasks.size()!=0){
			for (Task task:tasks){
				htmlContent.append(getComboOption(task.getName(), task.getId().toString()));
			}
		}	
		htmlContent.append("</select><input name=\"send\" type=\"submit\" class=\"inFormBtn\" value=\"Отправить\">");
		return htmlContent.toString();
	}

}
